package it.polimi.authentication.web.beans;

import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;

import it.polimi.TravelDream.BasePackageDTO;
import it.polimi.TravelDream.ExcursionDTO;
import it.polimi.TravelDream.FlightDTO;
import it.polimi.TravelDream.HotelDTO;
import it.polimi.TravelDream.PackageMgr;
import it.polimi.TravelDream.SearchEngine;
import it.polimi.TravelDream.SavedPackageDTO;
import it.polimi.TravelDream.UserMgr;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


@ManagedBean(name="searchBean")
@SessionScoped
public class SearchBean implements Serializable{

	
	    @EJB
		private SearchEngine searchEngine;
	    @EJB
		private UserMgr usermgr;
	    @EJB
	    private PackageMgr pckgmgr;
		
		private List<FlightDTO> filteredList;
		
		private List<HotelDTO> filteredListH;
		
		private List<ExcursionDTO> filteredListE;
		
		private List<BasePackageDTO> filteredListBP;
		
		private List<SavedPackageDTO> filteredListSP;
		
		private FlightDTO flight;
		
		private HotelDTO hotel;
		
		private ExcursionDTO excursion;
		
		private BasePackageDTO basePackage;
		
		private HotelDTO selectedHotel; 
		
		private FlightDTO selectedFlight;
		
		private ExcursionDTO selectedExcursion;
		
		private BasePackageDTO selectedBasePackage;
		
		private SavedPackageDTO selectedSavedPackage;
		
		private SavedPackageDTO savedPackage;
		
		private SavedPackageDTO selectedPackage;
		
		private FlightDTO selectedRetflight;
		
		private boolean renderBasePackage;
		private boolean renderFlight;
		private boolean renderHotel;
		private boolean renderExcursion;
		
		
		public SearchBean(){
			flight = new FlightDTO();
			filteredList = new ArrayList<FlightDTO>();
			
			hotel = new HotelDTO();
			filteredListH = new ArrayList<HotelDTO>();
			
			excursion = new ExcursionDTO();
			filteredListE = new ArrayList<ExcursionDTO>();
			
			basePackage = new BasePackageDTO();
			filteredListBP = new ArrayList<BasePackageDTO>();
			
			savedPackage = new SavedPackageDTO();
			filteredListSP = new ArrayList<SavedPackageDTO>();
			renderBasePackage=false;
			renderFlight=false;
			renderHotel=false;
			renderExcursion=false;
		}
		
		public FlightDTO getFlight() {
			return flight;
		}
		
		public void setFlight(FlightDTO flight) {
			this.flight = flight;
		}
		public String searchFlight() {
			filteredList.clear();
			filteredListH.clear();
			filteredListE.clear();
			filteredListBP.clear();
			renderBasePackage=false;
			renderFlight=true;
			renderHotel=false;
			renderExcursion=false;
			filteredList = searchEngine.searchFlightDTO(this.flight);
			return "results?faces-redirect=true";
		}

		
		public FlightDTO getSelectedFlight() {  
	        return selectedFlight;  
	    }  
	    public void setSelectedFlight(FlightDTO selectedFlight) {  
	        this.selectedFlight = selectedFlight;  
	    }  
		
		public HotelDTO getHotel(){
			return hotel;
		}
		
		public void setHotel(HotelDTO hotel) {
			this.hotel = hotel;
		}
		
		
		public String searchHotel(){
			filteredList.clear();
			filteredListH.clear();
			filteredListE.clear();
			filteredListBP.clear();
			renderBasePackage=false;
			renderFlight=false;
			renderHotel=true;
			renderExcursion=false;
			filteredListH = searchEngine.searchHotelDTO(this.hotel);
			return "results?faces-redirect=true";
			
		}
		
		
		  
		  public HotelDTO getSelectedHotel() {  
		        return selectedHotel;  
		    }  
		    public void setSelectedHotel(HotelDTO selectedHotel) {  
		        this.selectedHotel = selectedHotel;  
		    }  
		
		public ExcursionDTO getExcursion(){
			return excursion;
		}
		
		public void setExcursion(ExcursionDTO excursion) {
			this.excursion = excursion;
		}
		
		
		public String searchExcursion(){
			filteredList.clear();
			filteredListH.clear();
			filteredListE.clear();
			filteredListBP.clear();
			renderBasePackage=false;
			renderFlight=false;
			renderHotel=false;
			renderExcursion=true;
			filteredListE = searchEngine.searchExcursionDTO(this.excursion);
			return "results?faces-redirect=true";
			
		}

		public ExcursionDTO getSelectedExcursion() {  
	        return selectedExcursion;  
	    }  
	    public void setSelectedExcursion(ExcursionDTO selectedExcursion) {  
	        this.selectedExcursion = selectedExcursion;  
	    }  
		
		public SearchEngine getSE() {
		return searchEngine;
	}

		public void setSearchEngine(SearchEngine searchEngine) {
		this.searchEngine = searchEngine;
	}

		public List<FlightDTO> getFilteredList() {
			return filteredList;
		
		}
		
		public List<HotelDTO> getFilteredListH(){
			return filteredListH;
			
		}
		
		public List<ExcursionDTO> getFilteredListE(){
			return filteredListE;
			
		}
		
		public List<BasePackageDTO> getFilteredListBP(){
			return filteredListBP;
			
		}

		public void setFilteredList(java.util.ArrayList<FlightDTO> filteredList) {
		this.filteredList = filteredList;
	}
		
		public void setFilteredListH(java.util.ArrayList<HotelDTO> filteredListH) {
			this.filteredListH = filteredListH;
		}
			
		public void setFilteredListE(java.util.ArrayList<ExcursionDTO> filteredListE){
			this.filteredListE = filteredListE;
		}
		
		public void setFilteredListBP(java.util.ArrayList<BasePackageDTO> filteredListBP){
			this.filteredListBP = filteredListBP;
		}
		
		public String searchBasePackage(){
			filteredList.clear();
			filteredListH.clear();
			filteredListE.clear();
			filteredListBP.clear();
			renderBasePackage=true;
			renderFlight=false;
			renderHotel=false;
			renderExcursion=false;
			filteredListBP = searchEngine.searchBasePackageDTO(this.basePackage);
			
			return "results?faces-redirect=true";
		}
		
		public String deletePackage(){
			pckgmgr.deleteBasePackage(selectedBasePackage);
			filteredListBP.clear();
			filteredListBP = searchEngine.searchBasePackageDTO(this.basePackage);
			return"/admins/results?faces-redirect=true";
		}
		
		public String deleteSavedPackage(){
			pckgmgr.deleteSavedPackage(selectedSavedPackage);
			filteredListSP.clear();
			filteredListSP = searchEngine.searchSavedPackageByOwner();
			return "/users/home?faces-redirect=true";
		}
		
		public String viewBasePackage(){
			return "ShowPackage?faces-redirect";
			
		}
		
		
		public BasePackageDTO getSelectedBasePackage() {  
	        return selectedBasePackage;  
	    }  
	    public void setSelectedBasePackage(BasePackageDTO selectedBasePackage) {  
	        this.selectedBasePackage = selectedBasePackage;
	    }  
	    
	    public BasePackageDTO getBasePackage(){
			return basePackage;
		}
		
		public void setBasePackage(BasePackageDTO basepackage) {
			this.basePackage = basepackage;
		}

		public SavedPackageDTO getSavedPackage() {
			return savedPackage;
		}

		public void setSavedPackage(SavedPackageDTO savedPackage) {
			this.savedPackage = savedPackage;
		}
		
		
		
		
		public String searchSavedPackageByUser(){
			filteredListSP = searchEngine.searchSavedPackageByOwner();
			return "/users/home?faces-redirect=true";
		}

		public List<SavedPackageDTO> getFilteredListSP() {
			return filteredListSP;
		}

		public void setFilteredListSP(List<SavedPackageDTO> filteredListSP) {
			this.filteredListSP = filteredListSP;
		}

		public SavedPackageDTO getSelectedPackage() {
			return selectedPackage;
		}

		public void setSelectedPackage(SavedPackageDTO selectedPackage) {
			this.selectedPackage = selectedPackage;
		}

		public boolean isRenderBasePackage() {
			return renderBasePackage;
		}

		public void setRenderBasePackage(boolean renderBasePackage) {
			this.renderBasePackage = renderBasePackage;
		}

		public boolean isRenderFlight() {
			return renderFlight;
		}

		public void setRenderFlight(boolean renderFlight) {
			this.renderFlight = renderFlight;
		}

		public boolean isRenderHotel() {
			return renderHotel;
		}

		public void setRenderHotel(boolean renderHotel) {
			this.renderHotel = renderHotel;
		}

		public boolean isRenderExcursion() {
			return renderExcursion;
		}

		public void setRenderExcursion(boolean renderExcursion) {
			this.renderExcursion = renderExcursion;
		}

		public FlightDTO getSelectedRetflight() {
			return selectedRetflight;
		}

		public void setSelectedRetflight(FlightDTO selectedRetflight) {
			this.selectedRetflight = selectedRetflight;
		}

		public SavedPackageDTO getSelectedSavedPackage() {
			return selectedSavedPackage;
		}

		public void setSelectedSavedPackage(SavedPackageDTO selectedSavedPackage) {
			this.selectedSavedPackage = selectedSavedPackage;
		}

}
